package com.obrempon.credit.market.ui.activity

import android.text.SpannableString
import android.text.Spanned
import android.text.style.ForegroundColorSpan
import android.text.style.UnderlineSpan
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import androidx.viewpager.widget.ViewPager
import com.obrempon.credit.market.R
import com.obrempon.credit.market.databinding.ObremponGuideActBinding
import com.obrempon.credit.market.event.ObremponEventPointType
import com.obrempon.credit.market.httptest.HttpAct
import com.obrempon.credit.market.utils.ObremponTitleBarUtil
import com.obrempon.credit.market.utils.helper.ObremponAnalysisHelper
import com.obrempon.credit.market.utils.shared.ObremponSharedHelper
import com.obrempon.credit.market.utils.shared.ObremponSharedKey
import com.obrempon.credit.market.weight.viewpager.XBanner
import com.obrempon.credit.market.weight.viewpager.entity.GuideItem
import com.obrempon.credit.market.weight.viewpager.transformers.Transformer

/**
 * ObremponCredit
 * @Author wwdeng
 * @CreateTime: 2024/12/9 14:48
 * @Annotation：引导页
 */
class ObremponGuideAct: HttpAct() {
    lateinit var binding: ObremponGuideActBinding
    override fun takeObremponRootView(): View {
        binding = ObremponGuideActBinding.inflate(layoutInflater)
        return binding.root
    }

    override fun initObremponView() {
        ObremponTitleBarUtil.setStatusBarLightMode(this, true)
        rootViewBinding.rootLayout.setBackgroundColor(getColor(R.color.white))
        initGuideViewpager()
    }

    override fun initObremponListener() {
        binding.btnGuideNext.setOnClickListener {
            val curtIndex =  binding.guideViewPager.bannerCurrentItem
            if (curtIndex < 2){
                binding.guideViewPager.setBannerCurrentItem(curtIndex + 1,true)
            } else {
                onGuideStartNextStep()
            }
        }
    }

    override fun takeObremponData() {
        val guideDataList = arrayListOf<GuideItem>()
        for (i in 0..2){
            guideDataList.add(GuideItem())
        }
        binding.guideViewPager.setBannerData(R.layout.obrempon_guide_item,guideDataList)
    }

    private fun initGuideViewpager() {
        // 设置XBanner的页面切换特效
        binding.guideViewPager.setPageTransformer(Transformer.Default)
        // 设置XBanner页面切换的时间，即动画时长
        binding.guideViewPager.setPageChangeDuration(1000)
        binding.guideViewPager.setSlideScrollMode(1)
        binding.guideViewPager.setAllowUserScrollable(true)
        binding.guideViewPager.setAutoPlayAble(false)
        //显示指示器
        binding.guideViewPager.setPointsIsVisible(true)
        //加载广告图片
        binding.guideViewPager.loadImage(XBanner.XBannerAdapter { banner, model, view, position ->
            //1、此处使用的Glide加载图片，可自行替换自己项目中的图片加载框架
            //2、返回的图片路径为Object类型，你只需要强转成你传输的类型就行，切记不要胡乱强转！
            val txtGuideType = view.findViewById<TextView>(R.id.txtGuideType)
            val txtGuideMsg = view.findViewById<TextView>(R.id.txtGuideMsg)
            val imgGuide = view.findViewById<ImageView>(R.id.imgGuideIcon)

            val guideType: String
            var guideTypeIndex = 0
            val guideMsg: String
            val imgGuideIcon: Int
            when(position){
                0 -> {
                    guideType = String.format(getString(R.string.obrempon_guide_page1_type),getString(R.string.obrempon_guide_page1_type_key))
                    guideTypeIndex = guideType.indexOf(getString(R.string.obrempon_guide_page1_type_key))
                    guideMsg = getString(R.string.obrempon_guide_page1_msg)
                    imgGuideIcon = R.mipmap.obrempon_guide_page1
                }
                1 -> {
                    guideType = String.format(getString(R.string.obrempon_guide_page2_type),getString(R.string.obrempon_guide_page2_type_key))
                    guideTypeIndex = guideType.indexOf(getString(R.string.obrempon_guide_page2_type_key))
                    guideMsg = getString(R.string.obrempon_guide_page2_msg)
                    imgGuideIcon = R.mipmap.obrempon_guide_page2
                }
                else -> {
                    guideType = String.format(getString(R.string.obrempon_guide_page3_type),getString(R.string.obrempon_guide_page3_type_key))
                    guideTypeIndex = guideType.indexOf(getString(R.string.obrempon_guide_page3_type_key))
                    guideMsg = getString(R.string.obrempon_guide_page3_msg)
                    imgGuideIcon = R.mipmap.obrempon_guide_page3
                }
            }

            val spString = SpannableString(guideType)
            spString.setSpan(ForegroundColorSpan(getColor(R.color.color_ffff7029)), guideTypeIndex, guideType.length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
            //添加下划线
            spString.setSpan(UnderlineSpan(), guideTypeIndex, guideType.length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)

            txtGuideType.text = spString
            txtGuideMsg.text = guideMsg
            imgGuide.setImageResource(imgGuideIcon)
        })
        binding.guideViewPager.setOnPageChangeListener(object : ViewPager.OnPageChangeListener{
            override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int, ) {
            }
            override fun onPageSelected(position: Int) {
                if (position < 2){
                    binding.btnGuideNext.text = getString(R.string.obrempon_button_next_step)
                } else {
                    binding.btnGuideNext.text = getString(R.string.obrempon_button_get_started)
                }
            }
            override fun onPageScrollStateChanged(state: Int) {
            }
        })
    }

    /**
     * 下一步
     */
    private fun onGuideStartNextStep(){
        ObremponSharedHelper.addObremponSharedData(ObremponSharedKey.OBREMPON_SHOW_GUIDE_PAGE,true)
        //已登录 - 进入主页
        if (ObremponSharedHelper.queryObremponSharedBoolean(ObremponSharedKey.OBREMPON_LOGIN)){
            //初始化analysis lib 赋值 用户id 和 token
            ObremponAnalysisHelper.initAnalysisLibAtLoginSuccess()
            showLaunchMainPage()
            return
        }
        //进入登录页面
        showLaunchLoginPage()
    }

    /**
     * 进入主页
     */
    private fun showLaunchMainPage(){
        onIntent(ObremponMainAct::class.java)
        finish()
    }

    /**
     * 进入登录页
     */
    private fun showLaunchLoginPage() {
        onIntent(ObremponLoginAct::class.java)
        finish()
    }

    override fun onStart() {
        super.onStart()
        doEventTrackAction(ObremponEventPointType.PAGE.OBREMPON_PAGE_GUIDE_START)
    }

    override fun onDestroy() {
        super.onDestroy()
        doEventTrackAction(ObremponEventPointType.PAGE.OBREMPON_PAGE_GUIDE_END)
    }
}